package com.whut.googleplay.protocol;

import android.provider.Settings;

import com.whut.googleplay.bean.AppInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * ============================================================
 * <br/>
 * 作 者 :        xyb
 * <br/>
 * 版 本 :        1.0
 * <br/>
 * 创建日期 ：     2016-05-04 下午6:00
 * <br/>
 * 描 述 ：
 * <br/>
 * 修订历史 ：
 * <br/>
 * ============================================================
 **/
public class HomeProtocol extends BaseProtocol<List<AppInfo>> {

    private List<String> mPictureUrl;

    public List<String> getPictureUrl() {
        return mPictureUrl;
    }

    @Override
    public List<AppInfo> parseFromJson(String json) {
        System.out.println("json--->" + json);
        List<AppInfo> appInfoList = null;
        mPictureUrl = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.optJSONArray("picture");
            if (jsonArray != null) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    mPictureUrl.add(jsonArray.getString(i));
                }
            }

            appInfoList = new ArrayList<>();
            jsonArray=jsonObject.getJSONArray("list");
            for (int i=0;i<jsonArray.length();i++){
                JSONObject object = jsonArray.getJSONObject(i);
                AppInfo info = new AppInfo();

                info.setId(object.getInt("id"));
                info.setName(object.getString("name"));
                info.setPackageName(object.getString("packageName"));
                info.setIconUrl(object.getString("iconUrl"));
                info.setStars(object.getInt("stars"));
                info.setSize(object.getInt("size"));
                info.setDownloadUrl(object.getString("downloadUrl"));
                info.setDes(object.getString("des"));

                appInfoList.add(info);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return appInfoList;
    }

    @Override
    public String getKey() {
        return "home";
    }
}
